<?php

namespace app\admin\controller\aps;

use app\common\controller\Backend;

/**
 * 制程工序管理
 *
 * @icon fa fa-circle-o
 */
class Procedure extends Backend
{
    
    /**
     * Procedure模型对象
     * @var \app\admin\model\aps\Procedure
     */
    protected $model = null;

    protected $noNeedRight = ['zcgztj'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\aps\Procedure;
        $this->view->assign("stateList", $this->model->getStateList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        if (isset($_SERVER['HTTP_REFERER'])) {
            $url = $_SERVER['HTTP_REFERER'];
        } else  if (isset($_SERVER['REQUEST_URI'])) {
            $url = $_SERVER['REQUEST_URI'];
        }
        $mainprocessname = '';
        $mainprocess_id = 0;
        if (false !== strpos($url, '/ids/')) {
            $urls = explode('?', $url);
            $url = $urls[0];
            $urls = explode('/', $url);
            $size = count($urls);
            $mainprocess_id = $urls[$size - 1];
            $mainprocess = \app\admin\model\aps\Mainprocess::get($mainprocess_id);
            if ($mainprocess) {
                $mainprocessname = $mainprocess->name;
            }
        }

        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            if ($mainprocess_id > 0) {
                $total = $this->model
                    ->with(['devicetype','mainprocess'])
                    ->where($where)
                    ->where('mainprocess_id', $mainprocess_id)
                    ->order($sort, $order)
                    ->count();

                $list = $this->model
                    ->with(['devicetype','mainprocess'])
                    ->where($where)
                    ->where('mainprocess_id', $mainprocess_id)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            } else {
                $total = $this->model
                    ->with(['devicetype','mainprocess'])
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

                $list = $this->model
                    ->with(['devicetype','mainprocess'])
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            }

            foreach ($list as &$row) {

                $list2 = \app\admin\model\aps\Procedurestate::where('procedure_id', $row->id)->where('state', '<>', 0)->order('id desc')->limit(5)->select();
                foreach ($list2 as $index => $item) {
                    switch ($index) {
                        case 0:
                            $row['r0'] = $item->gongshi;
                            break;
                        case 1:
                            $row['r1'] = $item->gongshi;
                            break;
                        case 2:
                            $row['r2'] = $item->gongshi;
                            break;
                        case 3:
                            $row['r3'] = $item->gongshi;
                            break;
                        case 4:
                            $row['r4'] = $item->gongshi;
                            break;
                        default;
                    }
                }

            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }


        $this->view->assign("mainprocess_id", $mainprocess_id);
        $this->view->assign("mainprocessname", $mainprocessname);

        return $this->view->fetch();
    }


    public function zcgztj()
    {
        $startdate = $this->request->request('startdate', date("Y-m-d", strtotime("-7 days", time())));
        $enddate = $this->request->request('enddate', date('Y-m-d'));

        if ($this->request->isPost()) {
            $this->success("查询成功！");
        }

        $legendData = [];//所有制程名称
        $seriesData = [];//制程工时统计

        $this->relationSearch = true;
        $devicetypelist = \app\admin\model\aps\Devicetype::all();
        $totlgs = 0;
        foreach ($devicetypelist as $row) {
            $legendData[] = $row->name;
            //工序开始时间在[$startdate, $enddate]区间
            $list2 = \app\admin\model\aps\Procedurestate::with(['procedure'])
                ->where('procedure.devicetype_id', $row->id)
                ->where('procedurestate.state', '<>', 0)
                ->where('procedurestate.startdate', '>=', $startdate)
                ->where('procedurestate.startdate', '<=', $enddate)
                ->select();

            $value = 0;
            foreach ($list2 as $index => $item) {
                $value += $item->gongshi;
            }
//            $list2 = collection($list2)->toArray();

            $totlgs += $value;
            $seriesData[] = [
                'name' => $row->name,
                'value' => $value, //统计的工时
            ];

        }

        $this->view->assign("legendData", $legendData);
        $this->view->assign("seriesData", $seriesData);
        $this->view->assign("totlgs", $totlgs);

        $this->view->assign("startdate", $startdate);
        $this->view->assign("enddate", $enddate);
        return $this->view->fetch();
    }


}
